import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption('打砖块游戏')

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)

# 球的属性
ball_width = 20
ball_height = 20
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_x_change = 5 * random.choice([-1, 1])
ball_y_change = 5

# 挡板的属性
paddle_width = 100
paddle_height = 15
paddle_x = (screen_width // 2) - (paddle_width // 2)
paddle_y = screen_height - 50
paddle_x_change = 0

# 砖块属性
brick_width = 50
brick_height = 20
brick_color = BLUE
brick_rows = 5
brick_columns = 16
bricks = []

for row in range(brick_rows):
    brick_row = []
    for column in range(brick_columns):
        brick_x = column * (brick_width + 10) + 35
        brick_y = row * (brick_height + 5) + 50
        brick_rect = pygame.Rect(brick_x, brick_y, brick_width, brick_height)
        brick_row.append(brick_rect)
    bricks.append(brick_row)

# 游戏循环标志
running = True

# 游戏主循环
while running:
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 挡板移动
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and paddle_x > 0:
        paddle_x -= 7
    if keys[pygame.K_RIGHT] and paddle_x < screen_width - paddle_width:
        paddle_x += 7

    # 球的移动
    ball_x += ball_x_change
    ball_y += ball_y_change

    # 球的碰撞检测
    if ball_x <= 0 or ball_x >= screen_width - ball_width:
        ball_x_change *= -1
